/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmLogin.java
 *
 * Created on 01-06-2011, 14:37:51
 */
package Presentation;

import RSCall.DangNhapRSc;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;

/**
 *
 * @author Nixforest
 */
public class frmLogin extends javax.swing.JFrame {

    private String resource = "";
    /** Creates new form frmLogin */
    public frmLogin() {
        try {
            FileReader fr = new FileReader("config.dat");
            BufferedReader input = new BufferedReader(fr);
            input.readLine();
            String ui = input.readLine();
            input.readLine();
            input.readLine();
            int n = Integer.parseInt(ui);
            if (n == 0) {
                resource = "vn";
            }else resource = "en";
        } catch (IOException ex) {
            Logger.getLogger(frmLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tbxPassword = new javax.swing.JPasswordField();
        tbxUserName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnThoat = new javax.swing.JButton();
        btnDangNhap = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(java.util.ResourceBundle.getBundle("Presentation/" + resource).getString("login"));
        setAlwaysOnTop(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 14));
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("Presentation/vn"); // NOI18N
        jLabel1.setText(bundle.getString("USERNAME")); // NOI18N

        jLabel2.setFont(new java.awt.Font("Arial", 1, 14));
        jLabel2.setText(java.util.ResourceBundle.getBundle("Presentation/" + resource).getString("PASSWORD"));

        btnThoat.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnThoat.setText(java.util.ResourceBundle.getBundle("Presentation/" + resource).getString("close"));
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        btnDangNhap.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnDangNhap.setText(java.util.ResourceBundle.getBundle("Presentation/" + resource).getString("login"));
        btnDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDangNhapActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tbxPassword)
                            .addComponent(tbxUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(btnDangNhap)
                        .addGap(32, 32, 32)
                        .addComponent(btnThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tbxUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tbxPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThoat)
                    .addComponent(btnDangNhap))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDangNhapActionPerformed
        // TODO add your handling code here:
        if(tbxUserName.getText().isEmpty())
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + resource).getString("not enter username"));
        else
            if(tbxPassword.getPassword().length == 0)
                JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + resource).getString("not enter pass"));
            else
            {
                try
                {
                    DangNhapRSc _dangNhap = new DangNhapRSc();
                    if(_dangNhap.CheckDangNhap(tbxUserName.getText(), tbxPassword.getText()))
                    {
                        //JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + resource).getString("login success"));
                        this.setVisible(false);
                        frmMain frm = new frmMain();
                        frm.setVisible(true);
                        this.dispose();
                    }else 
                        JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + resource).getString("login fail"));
                }catch(Exception e){
                    JOptionPane.showMessageDialog(this, e.toString());
                }
            }
    }//GEN-LAST:event_btnDangNhapActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        JRootPane rootPane = this.getRootPane();
        rootPane.setDefaultButton(btnDangNhap);
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new frmLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDangNhap;
    private javax.swing.JButton btnThoat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField tbxPassword;
    private javax.swing.JTextField tbxUserName;
    // End of variables declaration//GEN-END:variables
}
